﻿using System.Windows;
using System.Threading;

namespace XamlMvp
{
    public partial class MvpApplicationServiceBase : IApplicationService 
    {
        private IDispatcher dispatcher;
        public IDispatcher Dispatcher
        {
            get
            {
                if (dispatcher == null)
                {
                    Interlocked.Exchange(ref dispatcher, new DispatcherWrapper(Deployment.Current.Dispatcher));
                }
                return dispatcher;
            }
            set
            {
                dispatcher = value;
            }
        }

        public void StartService(ApplicationServiceContext context)
        {
            InitializeHydrators(Binders.Hydrators);

            InitializePresenters(Binders.Presenters);

            ChainServices();
        }

        partial void ChainServices();
        partial void UnChainServices();

        protected virtual void InitializeHydrators(IHydratorBinder hydrators) { }

        protected virtual void InitializePresenters(IPresenterBinder presenters) { }

        public void StopService() 
        {
            UnChainServices();
        }
    }
}
